package pl.jtb.ctl.tabory.ui.security;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.log4j.Logger;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;

import pl.jtb.ctl.tabory.dao.api.services.IUserService;
import pl.jtb.ctl.tabory.dao.data.sl.UserRoleSL;
import pl.jtb.ctl.tabory.dao.data.vo.UserRoleVO;
import pl.jtb.ctl.tabory.dao.data.vo.UserVO;
import pl.jtb.ctl.tabory.dao.impl.mybatis.servicemanager.ServiceManager;
import pl.jtb.ctl.tabory.ui.osgi.UiBundleActivator;

public class SecurityUtils {

    private final static String GODMODE_USER = "god";
    private final static String GODMODE_USER_PASS = "godmode";

    private static final Logger logg = Logger.getLogger(SecurityUtils.class);

    public static boolean logonUser(String userName, String userPass) throws IOException, Exception {
        logg.debug("Performing login action with user: " + userName);
        boolean result = false;
        IUserService userService = ServiceManager.getInstance().getUserService();
        logg.debug("Querring DB for user with name: " + userName);
        UserVO user = userService.getUser(userName);
        if (user == null) {
            logg.debug("Found user for given name, checking permissions");
            if (GODMODE_USER.equals(userName) && GODMODE_USER_PASS.equals(userPass)) {
                logg.debug("ADMIN premission found");
                UserRoleVO userRole = ServiceManager.getInstance().getUserRoleService().getUserRole(UserRoleSL.ADMINISTRATOR.getId());
                user = new UserVO();
                user.setUserRoleVO(userRole);
                CredentialProvider.getInstance().setUser(user);
                result = true;
            } else {
                result = false;
                MessageDialog.openError(Display.getDefault().getActiveShell(), UiBundleActivator.getMessage("splash.dialog.title"), UiBundleActivator.getMessage("splash.dialog.message.wrongnamepass"));
            }
        } else {
            if (!user.getUserPass().equals(userPass)) {
                result = false;
                MessageDialog.openError(Display.getDefault().getActiveShell(), UiBundleActivator.getMessage("splash.dialog.title"), UiBundleActivator.getMessage("splash.dialog.message.wrongnamepass"));
                return result;
            }
            if (!user.isActive()) {
                result = false;
                MessageDialog.openError(Display.getDefault().getActiveShell(), UiBundleActivator.getMessage("splash.dialog.title"), UiBundleActivator.getMessage("splash.dialog.message.notactive"));
                return result;
            }
            if (!user.getUserRoleVO().getId().equals(UserRoleSL.ADMINISTRATOR.getId())) {
                Calendar calendarValidFrom = new GregorianCalendar();
                Calendar calendarValidUntil = new GregorianCalendar();
                Calendar calendarNow = new GregorianCalendar();
                calendarValidFrom.setTime(user.getDateValidFrom());
                calendarValidUntil.setTime(user.getDateValidUntil());
                calendarValidFrom.set(Calendar.HOUR_OF_DAY, 0);
                calendarValidFrom.set(Calendar.MINUTE, 0);
                calendarValidFrom.set(Calendar.SECOND, 0);
                calendarValidFrom.set(Calendar.MILLISECOND, 0);
                calendarValidUntil.set(Calendar.HOUR_OF_DAY, 0);
                calendarValidUntil.set(Calendar.MINUTE, 0);
                calendarValidUntil.set(Calendar.SECOND, 0);
                calendarValidUntil.set(Calendar.MILLISECOND, 0);
                calendarNow.set(Calendar.HOUR_OF_DAY, 0);
                calendarNow.set(Calendar.MINUTE, 0);
                calendarNow.set(Calendar.SECOND, 0);
                calendarNow.set(Calendar.MILLISECOND, 0);
                Date dateValidFrom = calendarValidFrom.getTime();
                Date dateValidUntil = calendarValidUntil.getTime();
                Date dateNow = calendarNow.getTime();
                if (!dateNow.before(dateValidFrom) && !dateNow.after(dateValidUntil)) {
                    result = true;
                    return result;
                } else {
                    MessageDialog.openError(Display.getDefault().getActiveShell(), UiBundleActivator.getMessage("splash.dialog.title"), UiBundleActivator.getMessage("splash.dialog.message.nottimevalid"));
                    return result;
                }
            }
            CredentialProvider.getInstance().setUser(user);
            result = true;
        }
        return result;
    }
}
